 //index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    hasUserInfo: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  onLoad: function() {
    wx.showLoading({
      title: '登录中',
    })
    if (app.globalData.userInfo) {
      //是否绑定
      this.isBound();

    } else {
      this.setData({
        hasUserInfo: false
      })
      setTimeout(() => {
        wx.hideLoading();
      }, 2000);
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo;
          this.setData({
            hasUserInfo: true
          })
          //是否绑定
          this.isBound();
        }
      })
    }
  },
  getUserInfo: function(e) {
    if (e.detail.userInfo) {
      //确认授权
      app.globalData.userInfo = e.detail.userInfo;
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })

      this.isBound();
    }

  },
  //判断是否绑定
  isBound() {
    wx.hideLoading();
    let that = this;
    if (app.globalData.isBound == 1) {
      //获取用户信息
      wx.getStorage({
        key: 'openId',
        success: function(res) {
          wx.switchTab({
            url: '/pages/homePage/homePage/homePage',
          })
        }
      })
    } else {
      wx.reLaunch({
        url: '/pages/login/register/register',
      })
    }
  }
})